package cn.dengta.webapp.im.entity;

import java.util.Date;

import cn.dengta.common.model.State;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.im.enums.GroupStatus;
import cn.dengta.webapp.im.enums.GroupUseStatus;
import cn.dengta.webapp.im.form.GroupForm;
import cn.dengta.webapp.user.entity.User;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

/**
 * IM 聊天群组
 *
 * @since 2022-06-08
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_group")
public class Group extends BizEntity {

	/** 网易云信的群组 tid */
	private String imId;
	/** 群名称 */
	private String name;
	/** 群图标 */
	private String icon;
	/** 群主ID（可以转让） */
	private Long ownerUserId;
	/** 群创建者ID */
	private Long addUserId;
	/** 加群模式：0=不用验证；1=需要验证；2=不允许任何人加入 */
	private Integer joinMode;
	/** 禁言类型：0=解除禁言；1=禁言普通成员；3=禁言整个群(包括群主) */
	private Integer muteType;
	/** 群消费钻石数量，每周清 0 */
	private Integer consumeCoin;
	/** 群人数 */
	private Integer count;
	/** 启用状态： 0=停用；1=启用；-1=解散； */
	private Integer enableStatus;
	/** 群VIP是否过期: 0=否；1=是 */
	private Integer vipExpire;
	/** 群类型: 0=免费群；1=vip群 */
	private Integer type;
	/** 系统免费群配置id */
	private Long sysConfigId;
	/** 最后活跃时间 */
	private Date lastActive;
	/** 红包金额 */
	private Integer redPackCoin;
	/** 礼物金额 */
	private Integer giftCoin;
	/** 白名单：0=否；1=是； */
	private Integer white;

	public static final String IM_ID = "im_id";
	public static final String NAME = "name";
	public static final String ICON = "icon";
	public static final String OWNER_USER_ID = "owner_user_id";
	public static final String ADD_USER_ID = "add_user_id";
	public static final String CONSUME_COIN = "consume_coin";
	public static final String MUTE_TYPE = "mute_type";
	public static final String JOIN_MODE = "join_mode";
	public static final String COUNT = "count";
	public static final String ENABLE_STATUS = "enable_status";
	public static final String RED_PACK_COIN = "red_pack_coin";
	public static final String GIFT_COIN = "gift_coin";
	public static final String VIP_EXPIRE = "vip_expire";
	public static final String TYPE = "type";
	public static final String SYS_CONFIG_ID = "sys_config_id";
	public static final String LAST_ACTIVE = "last_active";
	public static final String WHITE = "white";

	public static Group of(User user, GroupForm groupForm, Integer type, Long configId) {
		Group group = new Group();
		group.setName(groupForm.getName());
		group.setOwnerUserId(user.getId());
		group.setAddUserId(user.getId());
		group.setJoinMode(groupForm.getJoinMode());
		group.setIcon(StringUtils.isEmpty(groupForm.getIcon()) ? user.avatarOrDefault() : groupForm.getIcon());
		group.init(GroupStatus.VERIFY_WAIT.getValue(), State.PUBLIC);
		group.setEnableStatus(GroupUseStatus.ENABLED.getValue());
		group.initTime(new Date());
		group.setConsumeCoin(0);
		group.setVipExpire(0);
		group.setType(type);
		group.setSysConfigId(configId);
		return group;
	}

}
